/*
 *  JSONHelper.cpp
 *  AMPlugin
 *
 *  Created by Tiet Anh Vu on 5/24/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "AMGlobal.h"
#include "JSONHelper.h"
#include "HTTPRequestHelper.h"
#include "AMPLogDataRecord.h"
#include "AMPLogCenter.h"

//Magazine g_Magazines[10];


/********************************************************************************************************************
 *****Function: fillMagazine
 *****Creator:  Toan Nguyen
 *****IN: int index, const string &jsonString, bool loadImage, int min, int max, RangeProgressBar * BrowseProgress, bool EnableBar
 *****out: 
 *****Objective: fill magazine to browse issue list
 ********************************************************************************************************************/
void JSONHelper::fillMagazine(int index, const string &jsonString, bool loadImage, int min, int max, RangeProgressBar * BrowseProgress, bool EnableBar) {
	int issue_pos;
	//cout << "fillMagazine: "<< jsonString.c_str() << endl;
	Json::Value root;   // will contains the root value after parsing.
	Json::Reader reader;
	if(EnableBar) BrowseProgress->SetPosition(min);
	bool parsingSuccessful = reader.parse( jsonString, root );
	if ( !parsingSuccessful )
	{
		// report to the user the failure and their locations in the document.
		//std::cout  << "Failed to parse configuration\n" << endl;
		return;
	}
	
	// use magazine[0]
	//g_Magazines[0] = new Magazine();
	
	// magazine node
	const Json::Value magazineID = root["magazine_id"];
	const Json::Value magazineName = root["magazine_name"];
	//cout << "+++++++++++magazineID: " << magazineID.asString() << endl;
	g_Magazines[index]._id = magazineID.asString();
	g_Magazines[index]._name = magazineName.asString();
	cout << "magazine name: " << g_Magazines[index]._name << endl;

	// issue node
	const Json::Value issues = root["issues"];
	g_Magazines[index]._issues = new Issue[issues.size()];
	g_Magazines[index]._issueSize = issues.size();
	if(issues.size()!= 0) {
		issue_pos = (max - min) / issues.size();
		if(issue_pos==0) issue_pos = 4;
	}else
		issue_pos = (max - min);
	
	if(EnableBar) BrowseProgress->SetPosition(min + (issue_pos/3));
	for (int i = 0; i < issues.size(); i++) {
		//cout << "read issue content: " << i << endl;
		if(EnableBar) BrowseProgress->SetPosition(min + (i+1)*(issue_pos/2));
		Json::Value iID = issues[i]["issue_id"];
		Json::Value iName = issues[i]["issue_name"];
		Json::Value iPages = issues[i]["pages"];
		g_Magazines[index]._issues[i]._id = iID.asString();
		g_Magazines[index]._issues[i]._name = iName.asString();
		g_Magazines[index]._issues[i]._pageSize = iPages.size();
		g_Magazines[index]._issues[i]._pages = new Page[iPages.size()];
		
		if(EnableBar) BrowseProgress->SetPosition(min + (i+1)*issue_pos);
		int page_pos;
		if(iPages.size() != 0) {
			page_pos = issue_pos / iPages.size();
			if(page_pos<2) page_pos = 2;
		}else
			page_pos = 2;
		// page node
		for (int j = 0; j < iPages.size(); j++) {
			if(EnableBar) BrowseProgress->SetPosition(min + (i+1)*issue_pos + (j+1)*(page_pos/2));
			Json::Value pID = iPages[j]["page_id"];
			Json::Value pName = iPages[j]["page_name"];
			Json::Value pStatus = iPages[j]["page_status"];
			Json::Value pImageUrl = iPages[j]["page_preview_image"];
			Json::Value pStories = iPages[j]["stories"];
			Json::Value pNumber = iPages[j]["page_number"];
			g_Magazines[index]._issues[i]._pages[j]._id = pID.asString();
			g_Magazines[index]._issues[i]._pages[j]._name = pName.asString();
			//g_Magazines[index]._issues[i]._pages[j]._status = pStatus.asString();
			g_Magazines[index]._issues[i]._pages[j]._status = pStatus.asInt();
			g_Magazines[index]._issues[i]._pages[j]._imageUrl = pImageUrl.asString();
			g_Magazines[index]._issues[i]._pages[j]._number = pNumber.asInt();
			g_Magazines[index]._issues[i]._pages[j]._storySize = pStories.size();
			g_Magazines[index]._issues[i]._pages[j]._stories = new Story[pStories.size()];
			
			if(EnableBar) BrowseProgress->SetPosition(min + (i+1)*issue_pos + (j+1)*page_pos);
			string imgurl = pImageUrl.asString();
			if (imgurl.size() > 0) {
				//string tempstr = imgurl.substr(imgurl.find_last_of("/")+1);
				//cout << "page_preview_image " << tempstr << endl;
				if (loadImage == TRUE) {
					struct myprogress image_prog;
					image_prog.lastruntime = 0;
					image_prog.curl = nil;
					image_prog.MinPos = min + (i+1)*issue_pos + (j+1)*page_pos;
					image_prog.MaxPos = min + (i+1)*issue_pos + (j+2)*page_pos;
					image_prog.DownUp = DOWNLOADCOMMAND;
					image_prog.Progress = BrowseProgress;
					//cout << "image_prog.MinPos: " << image_prog.MinPos << endl;
					//cout << "image_prog.MaxPos: " << image_prog.MaxPos << endl;
					g_Magazines[index]._issues[i]._pages[j]._localImagePath = HTTPRequestHelper::getImage(imgurl, image_prog);
				}
				else {
					string filename = imgurl.substr(imgurl.find_last_of("/")+1);
					string sdkfilename = g_SDK_WorkspaceDir + ":" + filename;
					//cout << "sdkfilename: " << sdkfilename << endl;
					//g_Magazines[index]._issues[i]._pages[j]._localImagePath = sdkfilename;
					if(imgurl.find("[[]]") != string::npos)
					{
						string pathImg1 = imgurl.substr(0, imgurl.find("[[]]"));
						string pathImg2 = imgurl.substr(imgurl.find("[[]]")+4, imgurl.length());
						string imglink1 = pathImg1.substr(pathImg1.find_last_of("/")+1);
						string imglink2 = pathImg2.substr(pathImg2.find_last_of("/")+1);
						g_Magazines[index]._issues[i]._pages[j]._localImagePath = g_SDK_WorkspaceDir + ":" + imglink1 + "||||" + g_SDK_WorkspaceDir + ":" + imglink2;
					}else
					{
						g_Magazines[index]._issues[i]._pages[j]._localImagePath = sdkfilename;
					}
				}
			}
			else if(g_Magazines[index]._issues[i]._pages[j]._name.find("-") != string::npos) {
			
				g_Magazines[index]._issues[i]._pages[j]._localImagePath = g_SDK_WorkspaceDir + "||||" + g_SDK_WorkspaceDir;
			}else
			{
				g_Magazines[index]._issues[i]._pages[j]._localImagePath = g_SDK_WorkspaceDir;
			}
			int story_pos;
			if (pStories.size() != 0) {
				story_pos = page_pos / pStories.size();
				if(story_pos == 0) story_pos = 1;
			}else story_pos = 1;
			
			// story node
			int curr = 0;
			for (int k = 0; k < pStories.size(); k++) {
				//Json::Value sID = pStories[k]["story_id"];
				//Json::Value sName = pStories[k]["story_name"];
				//Json::Value sStatus = pStories[k]["story_status"];
				
				Json::Value sID = pStories[k]["id"];
				Json::Value sName = pStories[k]["name"];
				Json::Value sStatus = pStories[k]["status"];
				if(EnableBar) BrowseProgress->SetPosition(min + (i+1)*issue_pos + (j+1)*page_pos + (k+1)*story_pos);
				// for testing 
				//std::string s;
				//std::stringstream out;
				//out << k+100;
				//s = out.str();
				//string tempID = sID.asString() + s;
				//g_Magazines[0]._issues[i]._pages[j]._stories[k]._id = tempID;
				/////////////
				if (k == 0) {
					g_Magazines[index]._issues[i]._pages[j]._stories[0]._id = sID.asString();
					g_Magazines[index]._issues[i]._pages[j]._stories[0]._name = sName.asString();
					//g_Magazines[index]._issues[i]._pages[j]._stories[0]._status = sStatus.asString();
					g_Magazines[index]._issues[i]._pages[j]._stories[0]._status = sStatus.asInt();
					curr++;
				}else {
					int count = 0;
					for (int l = 0; l < k; l++) {
						if (g_Magazines[index]._issues[i]._pages[j]._stories[l]._id == sID.asString())
						{
							break;
						}else {
							count++;
						}
					}
					if(count == k) {
						g_Magazines[index]._issues[i]._pages[j]._stories[curr]._id = sID.asString();
						g_Magazines[index]._issues[i]._pages[j]._stories[curr]._name = sName.asString();
						//g_Magazines[index]._issues[i]._pages[j]._stories[curr]._status = sStatus.asString();
						g_Magazines[index]._issues[i]._pages[j]._stories[curr]._status = sStatus.asInt();
						curr++;
					}else {
						g_Magazines[index]._issues[i]._pages[j]._storySize -= 1;
					}
				}
			}
		}
	}
	if(EnableBar) BrowseProgress->SetPosition(max);
	//cout << "end fillMagazine" << endl;
}

/*
void JSONHelper::fillMagazine(int index, const string &jsonString, bool loadImage) {
	//cout << "fillMagazine"<< endl;
	Json::Value root;   // will contains the root value after parsing.
	Json::Reader reader;
	bool parsingSuccessful = reader.parse( jsonString, root );
	if ( !parsingSuccessful )
	{
		return;
	}
		
	// issue node
	const Json::Value issues = root["issues"];
	g_Magazines[index]._issues = new Issue[issues.size()];
	g_Magazines[index]._issueSize = issues.size();
	for (int i = 0; i < issues.size(); i++) {
		Json::Value iID = issues[i]["issue_id"];
		Json::Value iName = issues[i]["issue_name"];
		Json::Value iPackages = issues[i]["package"]; // a package will be display as a page
		//Json::Value iPages = issues[i]["pages"];
		g_Magazines[index]._issues[i]._id = iID.asString();
		g_Magazines[index]._issues[i]._name = iName.asString();
		g_Magazines[index]._issues[i]._pageSize = iPackages.size();
		g_Magazines[index]._issues[i]._pages = new Page[iPackages.size()];
		//cout << "issue " << g_Magazines[index]._issues[i]._id << " page size " << iPages.size() << endl;
		// page node
		for (int j = 0; j < iPackages.size(); j++) {
			//string jPageID;
			string jPageName;
			//string jPageStatus;
			Story tempStories[100];
			int tempStoriesCount = 0;
			Json::Value jPages = iPackages[j]["pages"];
			for (int k = 0; k < jPages.size(); k++) {
				Json::Value pName = jPages[k]["page_name"];
				Json::Value pImageUrl = jPages[k]["page_preview_image"];
				Json::Value pStories = jPages[k]["stories"];
				
				// pageName 
				if(k == jPages.size()-1) {
					jPageName += pName.asString();
				}
				else {
					jPageName += pName.asString() + ", ";
				}
				
				// preview image
				string imgurl = pImageUrl.asString();
				if (imgurl.size() > 0) {
					//string tempstr = imgurl.substr(imgurl.find_last_of("/")+1);
					//cout << "page_preview_image " << tempstr << endl;
					if (loadImage == TRUE) {
						g_Magazines[index]._issues[i]._pages[j]._localImagePath += HTTPRequestHelper::getImage(imgurl) + ";";
					}
					else {
						string filename = imgurl.substr(imgurl.find_last_of("/")+1);
						string sdkfilename = g_SDK_WorkspaceDir + ":" + filename;
						//cout << "sdkfilename: " << sdkfilename << endl;
						g_Magazines[index]._issues[i]._pages[j]._localImagePath += sdkfilename + ";";
					}
				}
				else {
					g_Magazines[index]._issues[i]._pages[j]._localImagePath += g_SDK_WorkspaceDir + ";";
				}
				
				// story
				for (int l = 0; l < pStories.size(); l++) {
					bool shouldAdd = TRUE;
					Json::Value lID = pStories[l]["id"];
					for (int l1 = 0; l1 < tempStoriesCount; l1++) {
						if (tempStories[l1]._id.compare(lID.asString()) == 0) {
							shouldAdd = FALSE;
							break;
						}
					}
					
					if (shouldAdd == TRUE) {
						Json::Value lName = pStories[l]["name"];
						Json::Value lStatus = pStories[l]["status"];
						tempStories[tempStoriesCount]._id = lID.asString();
						tempStories[tempStoriesCount]._name = lName.asString();
						tempStories[tempStoriesCount]._status = lStatus.asString();
						tempStoriesCount++;
					}
				} // end l loop
				
			} // end k loop
			
			int firstItem = 0;
			Json::Value pID = jPages[firstItem]["page_id"];
			Json::Value pStatus = jPages[firstItem]["page_status"];
			g_Magazines[index]._issues[i]._pages[j]._id = pID.asString();
			g_Magazines[index]._issues[i]._pages[j]._name = jPageName;
			g_Magazines[index]._issues[i]._pages[j]._status = pStatus.asString();
			g_Magazines[index]._issues[i]._pages[j]._storySize = tempStoriesCount;
			g_Magazines[index]._issues[i]._pages[j]._stories = new Story[tempStoriesCount];
			
			// story node
			for (int m = 0; m < tempStoriesCount; m++) {				
				g_Magazines[index]._issues[i]._pages[j]._stories[m]._id = tempStories[m]._id;
				g_Magazines[index]._issues[i]._pages[j]._stories[m]._name = tempStories[m]._name;
				g_Magazines[index]._issues[i]._pages[j]._stories[m]._status = tempStories[m]._status;
			}
		} // end j loop
	}
	//cout << "end fillMagazine" << endl;
}
*/


/********************************************************************************************************************
 *****Function: initMagazines
 *****Creator:  Toan Nguyen
 *****IN: const string &jsonString
 *****out: 
 *****Objective: init magazines
 ********************************************************************************************************************/
void JSONHelper::initMagazines(const string &jsonString) {
	Json::Value root;   // will contains the root value after parsing.
	Json::Reader reader;
	bool parsingSuccessful = reader.parse( jsonString, root );
	if ( !parsingSuccessful )
	{
		// report to the user the failure and their locations in the document.
		//std::cout  << "Failed to parse configuration\n" << reader.getFormattedErrorMessages();
		return;
	}
	
	// clear the last array
	if (g_Magazines != NULL) {
		delete [] g_Magazines;
		g_Magazines = NULL;
		g_Magazine_Count = 0;
	}
	
	const Json::Value magazines = root["magazines"];
	g_Magazines = new Magazine[magazines.size()];
	for (int i = 0; i < magazines.size(); i++) {
		Json::Value iID = magazines[i]["magazine_id"];
		Json::Value iName = magazines[i]["magazine_name"];
		g_Magazines[i]._id = iID.asString();
		g_Magazines[i]._name = iName.asString();
		g_Magazines[i]._issueSize = 0;
		//cout << "magazine id - name " << g_Magazines[i]._id << " - " << g_Magazines[i]._name << endl;
	}
	g_Magazine_Count = magazines.size();
	//cout << "#############g_Magazine_Count: " << g_Magazine_Count << endl;

}

/********************************************************************************************************************
 *****Function: imagesMapString
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: make JSON image map
 ********************************************************************************************************************/	
string JSONHelper::imagesMapString() {
	Json::Value array_node(Json::arrayValue);
	for (int i = 0; i < g_ImageInfo.size(); i++) {
		images_t iImgInfo = g_ImageInfo[i];
		if (iImgInfo.image_status != "added") {
			Json::Value image_node;
			image_node["story_id"] = iImgInfo.story_id;
			image_node["image_name"] = iImgInfo.image_name;
			image_node["image_caption"] = iImgInfo.image_caption;
			image_node["image_status"] = iImgInfo.image_status;
			array_node.append(image_node);	
		}
	}
	
	Json::Value root_node;
	root_node["images"] = array_node;
	
	//Json::StyledWriter writer;
	Json::FastWriter writer;
	string output = writer.write(root_node);	
	output.erase(std::remove(output.begin(), output.end(), '\n'), output.end());
	//cout << "-----------------------------imagesMapString: " << output << endl;
	return output;
}

/********************************************************************************************************************
 *****Function: storiesMapString
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: make JSON story map
 ********************************************************************************************************************/
string JSONHelper::storiesMapString() {
	Json::Value array_node(Json::arrayValue);
	for (int i = 0; i < g_TextFrameCount; i++) {
		Json::Value story_node;
		story_node["story_file"] = g_TextFrames[i].textframeUID;
		story_node["story_id"] = g_TextFrames[i].storyID;
		story_node["story_self"] = g_TextFrames[i].storySelf;
		array_node.append(story_node);
	} 
	Json::Value root_node;
	root_node["stories"] = array_node;
	
	//Json::StyledWriter writer;
	Json::FastWriter writer;
	string output = writer.write(root_node);
	output.erase(std::remove(output.begin(), output.end(), '\n'), output.end());
	return output;
}

/********************************************************************************************************************
 *****Function: linksMapString
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: make JSON links map
 ********************************************************************************************************************/
string JSONHelper::linksMapString() {
	Json::Value array_node(Json::arrayValue);
	for (int i = 0; i < g_LinkInfo.size(); i++) {
		links_t ilinkInfo = g_LinkInfo[i];
		
		Json::Value image_node;
		image_node["image_before"] = ilinkInfo.image_before;
		image_node["image_after"] = ilinkInfo.image_after;
		image_node["original_path"] = ilinkInfo.original_path;

		array_node.append(image_node);
	}
	
	Json::Value root_node;
	root_node["links_map"] = array_node;
	
	//Json::StyledWriter writer;
	Json::FastWriter writer;
	string output = writer.write(root_node);	
	output.erase(std::remove(output.begin(), output.end(), '\n'), output.end());
	//cout << "-----------------------------imagesMapString: " << output << endl;
	return output;
}

/********************************************************************************************************************
 *****Function: AMPluginSetInfoMap
 *****Creator:  Toan Nguyen
 *****IN: // [[package_id]] - [[session_id]] - [[username]] - [[domain]] - [[pages]] - [[resolution]] - [[preview_path]] - [[preview_name]] - \
 [[TextFrameCount]] - [[selectedtxtFrame]] - [[issue]] - [[Zip]] - [[sysLog]] - [[sdkLog]] - [[sysSpace]] - [[sdkSpace]] - [[CurTxtSelected]] - [[PrevTxtSelected]] -
 *****out: 
 *****Objective: make info for textframe
 ********************************************************************************************************************/ 
string JSONHelper::AMPluginSetInfoMap() {
	
	string textframeSize;
	stringstream outstr;
	outstr << g_TextFrameCount;
	textframeSize = outstr.str();
	
	string output = g_DocName + "[[package_id]]" +									// 1
					n_mSessionID + "[[session_id]]" +								// 2
					g_Username + "[[username]]" + 									// 3
					g_Domain + "[[domain]]" + 										// 4
					g_pageNumberCheckOut + "[[pages]]" + 							// 5
					g_Resolution + "[[resolution]]" + 								// 6
					g_CurPreviewImaPath + "[[preview_path]]" + 						// 7
					g_CurPreviewNodeName + "[[preview_name]]" + 					// 8
					textframeSize + "[[TextFrameCount]]" + 							// 9
					g_SelectedTextFrameUID + "[[selectedtxtFrame]]" + 				// 10
					g_Issueid + "[[issue]]" + 										// 11
					g_IDMLzip + "[[Zip]]" + 										// 11a
					g_System_logDirectory + "[[sysLog]]" + 							// 11b
					g_SDK_logDirectory + "[[sdkLog]]" + 							// 11c
					g_System_WorkspaceDir + "[[sysSpace]]" + 						// 11d
					g_SDK_WorkspaceDir + "[[sdkSpace]]" + 							// 11e
					g_pageCheckIn + "page";											// 11f
	
	outstr.str("");
	outstr << g_TextSelected;
	textframeSize = outstr.str() + "[[CurTxtSelected]]";
	output = output + textframeSize;
	
	outstr.str("");
	outstr << g_PrevTextSelected;
	textframeSize = outstr.str() + "[[PrevTxtSelected]]";
	output = output + textframeSize;
	/*	//do not open
	outstr.str("");
	outstr << g_CanCheckIn;
	textframeSize = outstr.str() + "[[CanCheckIn]]";
	output = output + textframeSize;
	*/
	string textframe = "";
	for (int k = 0; k < g_TextFrameCount; k++) {
		textframe = textframe + g_TextFrames[k].textframeUID + "[x]";		//a1
		textframe = textframe + g_TextFrames[k].storyID + "[x]";			//a2
		outstr.str("");
		outstr << g_TextFrames[k].isLocked;
		textframe = textframe + outstr.str() + "[x]";						//a3
		textframe = textframe + g_TextFrames[k].sessionID + "[x]";			//a4
		textframe = textframe + g_TextFrames[k].storyName + "[x]";			//a5
		outstr.str("");
		outstr << g_TextFrames[k].beginRemovePos;
		textframe = textframe + outstr.str() + "[x]";						//a6
		outstr.str("");
		outstr << g_TextFrames[k].endRemovePos;
		textframe = textframe + outstr.str() + "[x]";						//a7
		textframe = textframe + g_TextFrames[k].storySelf + "[x]";			//a8
		
	}
	output = output + textframe;
	
	// save g_assignmentPtr
	textframe = "";
	outstr.str("");
	outstr << g_assignmentPtr.size();
	output = output + outstr.str() + "+X+";									//b0
	for (int n = 0; n < g_assignmentPtr.size(); n++) {
		textframe = textframe + g_assignmentPtr[n].story_id + "-X-";		//b1
		outstr.str("");
		outstr << g_assignmentPtr[n]._status;
		textframe = textframe + outstr.str() + "=X=";						//b2
		textframe = textframe + g_assignmentPtr[n].storyUID + "-T-";		//b3
	}
	output = output + textframe;
	
	// save g_captionImg
	textframe = "";
	outstr.str("");
	outstr << g_captionImg.size();
	output = output + outstr.str() + "+w+";									//C0
	for (int n = 0; n < g_captionImg.size(); n++) {
		textframe = textframe + g_captionImg[n].caption_self + "-A-";		//C1
	}
	output = output + textframe;
	//SaveLogCenterData(output);
	return output;
}

/********************************************************************************************************************
 *****Function: AMPluginGetInfoMap
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: // [[package_id]] - [[session_id]] - [[username]] - [[domain]] - [[pages]] - [[resolution]] - \
 [[preview_path]] - [[preview_name]] - [[TextFrameCount]] - [[selectedtxtFrame]] - [[issue]] - [[Zip]] - \
 [[sysLog]] - [[sdkLog]] - [[sysSpace]] - [[sdkSpace]] - [[CurTxtSelected]] - [[PrevTxtSelected]] -
 *****Objective: get info from textframe
 ********************************************************************************************************************/
void JSONHelper::AMPluginGetInfoMap(const string &buffer) {
	string _error_info;
	string iContent = buffer;
	string user, url;
	if(buffer == "")
		return;
	// package id
	size_t start_info = iContent.find("[[package_id]]");		//1
	if(start_info != string::npos) {
	 	g_DocName = g_Package_id = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 14);
	}
		
	// session id
	start_info = iContent.find("[[session_id]]");				//2
	if(start_info != string::npos) {
		n_mSessionID = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 14);
	}
		
	// username
	start_info = iContent.find("[[username]]");					//3
	if(start_info != string::npos) {
		user = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 12);
	}
		
	// domain
	start_info = iContent.find("[[domain]]");					//4
	if(start_info != string::npos) {
		url = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 10);
	}
	if(user != g_Username && url != g_Domain &&
		g_DocName != "" && g_Domain != "")
		return;
	
	// pages
	start_info = iContent.find("[[pages]]");					//5
	if(start_info != string::npos) {
		g_pageNumberCheckOut = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 9);
		g_pageEditing = g_pageNumberCheckOut;
	}
		
	// resolution
	start_info = iContent.find("[[resolution]]");				//6
	if(start_info != string::npos) {
		g_Resolution = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 14);
	}
		
	// preview_path
	start_info = iContent.find("[[preview_path]]");				//7
	if(start_info != string::npos) {
		g_CurPreviewImaPath = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 16);
	}
		
	// preview_name
	start_info = iContent.find("[[preview_name]]");				//8
	if(start_info != string::npos) {
		g_CurPreviewNodeName = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 16);
	}
	
	// Text Frame Count
	start_info = iContent.find("[[TextFrameCount]]");			//9
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 18);
		g_TextFrameCount = atoi(FrameCount.c_str());
	}
	
	// [[selectedtxtFrame]]
	start_info = iContent.find("[[selectedtxtFrame]]");			//10
	if(start_info != string::npos) {
		g_SelectedTextFrameUID = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 20);
	}
	
	// [[issue]]
	start_info = iContent.find("[[issue]]");			//11
	if(start_info != string::npos) {
		g_Issueid = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 9);
	}
	
	// [[Zip]]
	start_info = iContent.find("[[Zip]]");				//11a
	if(start_info != string::npos) {
		g_IDMLzip = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 7);
	}
	
	// [[sysLog]]
	start_info = iContent.find("[[sysLog]]");			//11b
	if(start_info != string::npos) {
		g_System_logDirectory = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 10);
	}
	
	// [[sdkLog]]
	start_info = iContent.find("[[sdkLog]]");			//11c
	if(start_info != string::npos) {
		g_SDK_logDirectory = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 10);
	}
	
	// [[sysSpace]]
	start_info = iContent.find("[[sysSpace]]");			//11d
	if(start_info != string::npos) {
		g_System_WorkspaceDir = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 12);
	}
	
	// [[sdkSpace]]
	start_info = iContent.find("[[sdkSpace]]");			//11e
	if(start_info != string::npos) {
		g_SDK_WorkspaceDir = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 12);
	}
	
	// page
	start_info = iContent.find("page");					//11f
	if(start_info != string::npos) {
		g_pageCheckIn = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 4);
	}
	
	// [[CurTxtSelected]]
	start_info = iContent.find("[[CurTxtSelected]]");			//12
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 18);
		g_TextSelected = atoi(FrameCount.c_str());
	}
	
	// [[PrevTxtSelected]]
	start_info = iContent.find("[[PrevTxtSelected]]");			//13
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 19);
		g_PrevTextSelected = atoi(FrameCount.c_str());
	}
	/*
	// [[CanCheckIn]]
	start_info = iContent.find("[[CanCheckIn]]");				//14
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 14);
		g_CanCheckIn = atoi(FrameCount.c_str());
	}
	*/
	if (g_TextFrames != NULL) {
		delete [] g_TextFrames;
	}
	g_TextFrames = new textframes_t[g_TextFrameCount];
	string temp;
	for (int k = 0; k < g_TextFrameCount; k++) {
		// textframeUID
		start_info = iContent.find("[x]");									//a1
		if(start_info != string::npos) {
			g_TextFrames[k].textframeUID = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		// storyID
		start_info = iContent.find("[x]");									//a2
		if(start_info != string::npos) {
			g_TextFrames[k].storyID = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		// locked
		start_info = iContent.find("[x]");									//a3
		if(start_info != string::npos) {
			temp = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
			g_TextFrames[k].isLocked = atoi(temp.c_str());
		}
		// session id
		start_info = iContent.find("[x]");									//a4
		if(start_info != string::npos) {
			g_TextFrames[k].sessionID = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		// storyName
		start_info = iContent.find("[x]");									//a5
		if(start_info != string::npos) {
			g_TextFrames[k].storyName = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		// g_TextFrames[k].beginRemovePos
		start_info = iContent.find("[x]");									//a6
		if(start_info != string::npos) {
			temp = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
			g_TextFrames[k].beginRemovePos = atoi(temp.c_str());
		}
		// g_TextFrames[k].endRemovePos
		start_info = iContent.find("[x]");									//a7
		if(start_info != string::npos) {
			temp = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
			g_TextFrames[k].endRemovePos = atoi(temp.c_str());
		}
		// storySelf
		start_info = iContent.find("[x]");									//a8
		if(start_info != string::npos) {
			g_TextFrames[k].storySelf = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
	}
	g_issueEditing = g_Issueid;
	
	if(g_assignmentPtr.size() > 0) {
		g_assignmentPtr.clear();
	}
	
	int assCount = 0;
	start_info = iContent.find("+X+");										//b0
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 3);
		assCount = atoi(FrameCount.c_str());
	}
	
	pt_assignStr addedStoryass;
	for (int n = 0; n < assCount; n++) {
		//cout << "_assCount:" << assCount  << " :" << n << endl;
		start_info = iContent.find("-X-");									//b1
		if(start_info != string::npos) {
			addedStoryass.story_id = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		//cout << "_story_id:" << addedStoryass.story_id << endl;
		int status = 0;
		start_info = iContent.find("=X=");									//b2
		if(start_info != string::npos) {
			string FrameCount = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
			status = atoi(FrameCount.c_str());
		}
		addedStoryass._status = status;
		//cout << "_status:" << addedStoryass._status << endl;
		
		start_info = iContent.find("-T-");									//b3
		if(start_info != string::npos) {
			addedStoryass.storyUID = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		addedStoryass.story_assignment = UIDRef::gNull;
		//cout << "_storyUID:" << addedStoryass.storyUID << endl;
		g_assignmentPtr.push_back(addedStoryass);
	}
	
	if(g_captionImg.size() > 0) {
		g_captionImg.clear();
	}
	
	assCount = 0;
	start_info = iContent.find("+W+");										//C0
	if(start_info != string::npos) {
		string FrameCount = iContent.substr(0, start_info);
		iContent = iContent.substr(start_info + 3);
		assCount = atoi(FrameCount.c_str());
	}
	
	caption_t addedCaptionImg;
	for (int n = 0; n < assCount; n++) {
		//cout << "_assCount:" << assCount  << " :" << n << endl;
		start_info = iContent.find("-A-");									//C1
		if(start_info != string::npos) {
			addedCaptionImg.caption_self = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 3);
		}
		g_captionImg.push_back(addedCaptionImg);
	}
	
	HandleSelectedStatusFrame = kNoStatus;
	_error_info = "AMPlugin get info map successful";
	SaveLogCenterData(_error_info);
}

/********************************************************************************************************************
 *****Function: FindParamID
 *****Creator:  Toan Nguyen
 *****IN: const string &buffer, string param_find
 *****out: // [[package_id]] - [[session_id]] - [[username]] - [[domain]] - [[pages]] - [[resolution]] - \
 [[preview_path]] - [[preview_name]] - [[TextFrameCount]] - [[selectedtxtFrame]] - [[issue]] - [[Zip]] - \
 [[sysLog]] - [[sdkLog]] - [[sysSpace]] - [[sdkSpace]] - [[CurTxtSelected]] - [[PrevTxtSelected]] -
 *****Objective: find a param info from textframe
 ********************************************************************************************************************/
string JSONHelper::FindParamID(const string &buffer, string param_find) {
	if(param_find == "")
		return "";
	
	string str = "";
	string iContent = buffer;	
	do {
		// package id
		size_t start_info = iContent.find("[[package_id]]");
		str = iContent.substr(0, start_info);
		if(start_info != string::npos) {
			iContent = iContent.substr(start_info + 14);
			if(param_find == "[[package_id]]")
				break;
		}
		// session_id
		start_info = iContent.find("[[session_id]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 14);
			if(param_find == "[[session_id]]")
				break;
		}
		// username
		start_info = iContent.find("[[username]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 12);
			if(param_find == "[[username]]")
				break;
		}
		// domain
		start_info = iContent.find("[[domain]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 10);
			if(param_find == "[[domain]]")
				break;
		}
		// pages
		start_info = iContent.find("[[pages]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 9);
			if(param_find == "[[pages]]")
				break;
		}
		// resolution
		start_info = iContent.find("[[resolution]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 14);
			if(param_find == "[[resolution]]")
				break;
		}
		// preview_path
		start_info = iContent.find("[[preview_path]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 16);
			if(param_find == "[[preview_path]]")
				break;
		}
		// preview_name
		start_info = iContent.find("[[preview_name]]");
		if(start_info != string::npos) {
			str = iContent.substr(0, start_info);
			iContent = iContent.substr(start_info + 16);
			if(param_find == "[[preview_name]]")
				break;
		}
	}while(kFalse);
	//cout << "param="<< str << endl;
	return str;
}